#!/usr/bin/tclsh

proc Error {S} {
puts stderr $S
}


proc Usage {} {
Error {
Usage: ltsconf [command [args]]
Commands:
    help       - help [command]
    get        - get key value
    set        - set or create key value
    del|delete - delete key or section
    list       - list sections or keys of section}
exit 1
}


proc Help {Cmds} {
if {[llength "$Cmds"]==0} {::Usage}
foreach C $Cmds {
    switch -exact $C {
	help	{puts {help COMMAND - help for COMMAND}}
	set	{puts {set KEY VALUE SECTION - set KEY to VALUE in SECTION}}
	get	{puts {get KEY SECTION - get KEY from SECTION}}
	delete	-
	del	{puts {del|delete [KEY] SECTION - delete [KEY from] SECTION}}
	list	{puts {list [SECTION] - list [KEYS of] SECTION[S]}}
	default	{
	    Error "Unknown command \"$C\"!"
	    exit 21
	    }
    }
}
exit 0
}


proc SetParam {S P V} {
if {![::conffile::exists $::conf $S]} {
    Error "Section \[$S\] don't exist!"
    Error "Created section \[$S\]"
}
::conffile::set $::conf $S $P "$V"
::conffile::commit $::conf
Error "Set key $P in section \[$S\] to $V"
}


proc GetParam {S P} {
if {[::conffile::exists $::conf $S]} {
    if {[::conffile::exists $::conf $S $P]} {
	puts "Key $P in section \[$S\] is [::conffile::value $::conf $S $P]"
    } else {
	Error "Key $P in section \[$S\] don't exist!"
	if {[::conffile::exists $::conf $::GlobalSection $P]} {
	    puts "Key $P in section \[$::GlobalSection\] is [::conffile::value $::conf $::GlobalSection $P]"
	} else {
	    Error "Key $P in section \[$::GlobalSection\] don't exist!"
	}
    }
} else {
    Error "Section \[$S\] don't exist!"
}
}


proc DelParam {S P} {
if {[string length "$S"]==0} {
    set S "$P"
    set P ""
}
if {[::conffile::exists $::conf $S]} {
    if {[string length "$P"]==0} {
	::conffile::delete $::conf $S
	::conffile::commit $::conf
	Error "Deleted section \[$S\]"
    } elseif {[::conffile::exists $::conf $S $P]} {
	::conffile::delete $::conf $S $P
	::conffile::commit $::conf
	Error "Deleted key $P in section \[$S\]"
    } else {
	Error "Key $P in section \[$S\] don't exist!"
    }
} else {
    Error "Section \[$S\] don't exist!"
}
}


proc List {S} {
if {[string length "$S"]==0} {
    foreach S [::conffile::sections $::conf] {puts $S}
} else {
    if {[::conffile::exists $::conf $S]} {
	foreach K [::conffile::keys $::conf $S] {
	    puts "$K=[::conffile::value $::conf $S $K]"
	}
    } else {
	Error "Section \[$S\] don't exist!"
    }
}
}


if {$argc==0} Usage

set Cmd [lindex $argv 0]

set ConfFile "/etc/lts.conf"
set GlobalSection "default"

#package require conffile
source /usr/share/ltsp/conffile.tcl

set ::conffile::commentchar "#"
set conf [::conffile::open $ConfFile]
set ::conffile::indentsize 4

switch -exact "$Cmd" {
    help {
	Help [lrange $argv 1 end]
	}
    set {
	SetParam [lindex $argv 3] [lindex $argv 1] [lindex $argv 2]
	}
    get {
	GetParam [lindex $argv 2] [lindex $argv 1]
	}
    delete -
    del {
	DelParam [lindex $argv 2] [lindex $argv 1]
	}
    list {
	List [lindex $argv 1]
	} 
    default {
	Error "Unknown command \"$Cmd\""
	exit 11
	}
}

::conffile::close $conf
